package com.zhudi.develop_demo_as.ui.activity.pic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.zhudi.develop_demo_as.BaseActivity;
import com.zhudi.develop_demo_as.R;
import com.zhudi.develop_demo_as.ui.fragment.pic.ImagePagerFragment;

import java.util.List;


/**
 * Created by donglua on 15/6/24.
 */
public class PhotoPagerActivity extends BaseActivity implements View.OnClickListener{
  private Context context;
  private TextView tvTitle;
  private TextView tvFinish;
  private ImagePagerFragment pagerFragment;
  public final static String EXTRA_CURRENT_ITEM = "current_item";
  public final static String EXTRA_PHOTOS = "photos";

  @Override
  protected int getLayoutView() {
    return R.layout.activity_photo_pager;
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = this;
    initView();
    setListener();
  }

  private void initView() {
    tvTitle = (TextView) findViewById(R.id.tvTitle);
    tvFinish = (TextView) findViewById(R.id.tvFinish);
    setText(tvFinish,"删除");

    int currentItem = getIntent().getIntExtra(EXTRA_CURRENT_ITEM, 0);
    List<String> paths = getIntent().getStringArrayListExtra(EXTRA_PHOTOS);

    pagerFragment = (ImagePagerFragment) getSupportFragmentManager().findFragmentById(R.id.photoPagerFragment);
    pagerFragment.setPhotos(paths, currentItem);


    pagerFragment.getViewPager().addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        updateTitle();
      }

      @Override
      public void onPageSelected(int i) {

      }

      @Override
      public void onPageScrollStateChanged(int i) {

      }
    });
  }

  private void setListener() {
    tvFinish.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
      switch (v.getId()){
        case R.id.tvFinish:
          final int index = pagerFragment.getCurrentItem();
          final String deletedPath = pagerFragment.getPaths().get(index);
          //Snackbar snackbar = Snackbar.make(pagerFragment.getView(), R.string.deleted_a_photo,Snackbar.LENGTH_LONG);
          new MaterialDialog.Builder(this)
                  .content(R.string.confirm_to_delete)
                  .positiveText(R.string.agree)
                  .negativeText(R.string.disagree)
                  .callback(new MaterialDialog.ButtonCallback() {
                    @Override
                    public void onPositive(MaterialDialog dialog) {
                      //dialogInterface.dismiss();
                      if (pagerFragment.getPaths().size() <= 1) {
                        setResult(RESULT_OK);
                        finish();
                      } else {
                        pagerFragment.getPaths().remove(index);
                        pagerFragment.getViewPager().removeViewAt(index);
                        pagerFragment.getViewPager().getAdapter().notifyDataSetChanged();
                      }
                    }

                    @Override
                    public void onNegative(MaterialDialog dialog) {
                    }
                  })
                  .show();
          break;
      }
  }

//
//
//  @Override
//  public boolean onOptionsItemSelected(MenuItem item) {
//
//    if (item.getItemId() == android.R.id.home) {
//
//      Intent intent = new Intent();
//      intent.putExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS, pagerFragment.getPaths());
//      setResult(RESULT_OK, intent);
//      finish();
//      return true;
//    }
//
//    if (item.getItemId() == R.id.delete) {
//      final int index = pagerFragment.getCurrentItem();
//
//      final String deletedPath =  pagerFragment.getPaths().get(index);
//
//      Snackbar snackbar = Snackbar.make(pagerFragment.getView(), R.string.deleted_a_photo,
//          Snackbar.LENGTH_LONG);
//
//      if (pagerFragment.getPaths().size() <= 1) {
//
//        // show confirm dialog
//        new AlertDialog.Builder(this)
//            .setTitle(R.string.confirm_to_delete)
//            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
//              @Override public void onClick(DialogInterface dialogInterface, int i) {
//                dialogInterface.dismiss();
//                setResult(RESULT_OK);
//                finish();
//              }
//            })
//            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
//              @Override public void onClick(DialogInterface dialogInterface, int i) {
//                dialogInterface.dismiss();
//              }
//            })
//            .show();
//
//      } else {
//
//        snackbar.show();
//
//        pagerFragment.getPaths().remove(index);
//        pagerFragment.getViewPager().removeViewAt(index);
//
//        pagerFragment.getViewPager().getAdapter().notifyDataSetChanged();
//      }
//
//      snackbar.setAction(R.string.undo, new View.OnClickListener() {
//        @Override public void onClick(View view) {
//          if (pagerFragment.getPaths().size() > 0) {
//            pagerFragment.getPaths().add(index, deletedPath);
//          } else {
//            pagerFragment.getPaths().add(deletedPath);
//          }
//          pagerFragment.getViewPager().getAdapter().notifyDataSetChanged();
//          pagerFragment.getViewPager().setCurrentItem(index, true);
//        }
//      });
//
//      return true;
//    }
//
//    return super.onOptionsItemSelected(item);
//  }
//
  public void updateTitle() {
    String content = getString(R.string.image_index);
    tvTitle.setText(String.format(content,pagerFragment.getViewPager().getCurrentItem() + 1,pagerFragment.getPaths().size()));
  }

  @Override
  public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
      Intent intent = new Intent();
      intent.putExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS, pagerFragment.getPaths());
      setResult(RESULT_OK, intent);
      finish();
      return true;
    }
    return super.dispatchKeyEvent(event);
  }


}
